<form class="layui-form" lay-filter="form" id="form">
    <div class="layui-form-item">
        <label class="layui-form-label label-required-next">活动名称</label>
        <div class="layui-input-inline">
            <input type="text" name="title" lay-verify="required" lay-reqText="活动名称不能为空" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label label-required-next">商家</label>
        <div class="layui-input-inline">
            <script type="text/html" template lay-url="{{ layui.setter.apiHost }}/seller/seller/select" lay-done="layui.form.render();">
                <select name="seller_id" lay-filter="seller_id" lay-search="" lay-verify="required" lay-reqText="请选择商家">
                    <option value=""></option>
                    {{# layui.each(d.data, function(index, item){ }}
                    <option value="{{ item.id }}" {{# if(item.id== d.params.seller_id){ }}selected{{# } }}>{{ item.title }}</option>
                    {{# }); }}
                </select>
            </script>
        </div>
    </div>
    <div class="layui-form-item" id="select_at">
        <label class="layui-form-label label-required-next">起止时间</label>
        <div class="layui-input-inline">
            <input type="text" name="start_at" id="start_at" autocomplete="off" class="layui-input" readonly>
        </div>
        <div class="layui-form-mid layui-word-aux">-</div>
        <div class="layui-input-inline">
            <input type="text" name="end_at" id="end_at" autocomplete="off" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label label-required-next">商品</label>
        <div class="layui-input-block">
            <select name="goods_id" lay-filter="goods_id" lay-search="" lay-verify="required" lay-reqText="请选择商品">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <input type="hidden" name="id" value="">
            <input type="button" lay-submit lay-filter="form-submit" value="确认" class="layui-btn">
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <span class="layui-font-red">注意：修改了商品后这边必须点一下同步库存，否则商品库存不会同步</span>
        </div>
    </div>
</form>
<script type="text/html" template lay-done="layui.data.sendParams(d.params);"></script>
<script>
    layui.data.sendParams = function (params) {
        layui.use(function () {
            let $ = layui.$,
                form = layui.form,
                common = layui.common,
                laydate = layui.laydate,
                model_url = '/market/seckill';

            //日期
            laydate.render({
                elem: '#select_at',
                type: 'datetime',
                range: ['#start_at', '#end_at']
            });


            form.on('select(seller_id)', function (data) {
                get_goods(data.value);
            });

            //选择商品
            function get_goods(seller_id, goods_id) {
                let result = common.ajax(model_url + '/search', {seller_id: seller_id});
                if (result) {
                    let html = '<option value="">请选择</option>';
                    $.each(result.data, function (index, item) {
                        let selected = '';
                        if (item.id == goods_id) {
                            selected = 'selected';
                        }
                        html += '<option value="' + item.id + '" ' + selected + '>' + item.id + '、' + item.title + '</option>';
                    })
                    $('[name="goods_id"]').html(html);
                }
                form.render('select');
            }

            if (params.seller_id) {
                get_goods(params.seller_id, params.goods_id);
            }
        });
    }
</script>